package leetcode_1501_1600;

public class LeeCode_1539 {
    public static void main(String[] args) {
        System.out.println(findKthPositive(new int[]{2, 3, 4, 7, 11}, 5));
        System.out.println(findKthPositive(new int[]{1,2,3,4}, 2));
    }
    private static int findKthPositive(int[] arr, int k) {
        int pre = 1, c = 0;
        for (int num : arr) {
            if (num > pre){
                int cha = num - pre;
                if (c + cha >= k){
                    return pre + (k - c - 1);
                }
                c += cha;
                pre = num + 1;
            }else {
                pre++;
            }
        }
        return pre + (k - c - 1);
    }
}
